/**
 * Elfinder js module for green CMS
 *
 * JQUERY versions 1.7+
 * 
 * Green CMS - Content Management System and Framework Powerfull by Cakephp
 * Copyright 2012, GREEN GLOBAL CO., LTD (toancauxanh.vn)
 * 
 * jQuery(tm) Copyright 2011, John Resig http://jquery.org/license
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @author        Technology Lab No.I <tech1@toancauxanh.vn>
 * @link          
 * @package       Green.Js
 * @since         Green v 1.0
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

Green.Elfinder = Green.Elfinder || {};
(function ($) {
    var $this = Green.Elfinder;
    Green.attach("Elfinder",{
        setup:function(){
            $('form input.file-upload').prop('readonly' , true).click(function(){
                $this.show($(this));
            });
        }
    });
    $.extend($this, {
        _instance : null,
        _activated : null,
        /**
         * Copy
         */
        show:function($element){
            $this._activated  = $element;
            var instance = $this.getInstance();
            instance.dialog('open');
        },
        /**
         * Edit
         */
        hide:function(file){
            var instance = $this.getInstance();
            $this._activated.val(String(file).replace(/^\/img\//, ''));
            instance.dialog('close');
        },
        /**
         * Get element instance
         */
        getInstance:function(){
            if($this._instance){
                return $this._instance;
            }
            $this._instance = $('<div id="dialog-elfinder"></div>').appendTo('body');
            $this._instance.elfinder({
                resizable : false,
                getFileCallback: function(file){
                    $this.hide(file);
                },
                url : '/js/elfinder/php/connector.php',
                lang: 'vi'
            });
            $this._instance.dialog({
                title : 'Files Management',
                width: 960,
                autoOpen : true,
                dialogClass:'std42-dialog',
                maxHeight : false,
                modal: true,
                resizable : true
            }).css({
                overflow:'visible',
                height : 'auto'
            });
            return $this._instance;
        }
    });

})(jQuery);